package com.dky.satell.rocketmq.producer;

import com.aliyun.openservices.ons.api.ONSFactory;
import com.aliyun.openservices.ons.api.Producer;
import com.aliyun.openservices.ons.api.PropertyKeyConst;
import com.dky.satell.config.RocketMQConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Properties;

/**
 * rocketmq生产者启动初始化类
 *
 * @author sixmonth
 * @Date 2019年1月17日
 */
@Slf4j
@Component
public class RocketmqProducerInit {

    @Autowired
    private RocketMQConfig rocketMQConfig;

    private Producer producer;

//    @PostConstruct
    public void init() {
        log.info("[begin]初始化启动RocketMQ生产者！");
        // producer 实例配置初始化
        Properties properties = rocketMQConfig.getMqPropertie();
        //设置发送超时时间，单位毫秒
        properties.setProperty(PropertyKeyConst.SendMsgTimeoutMillis, "3000");
        producer = ONSFactory.createProducer(properties);
        // 在发送消息前，初始化调用start方法来启动Producer，只需调用一次即可，当项目关闭时，自动shutdown
        producer.start();
        log.info("[end]初始化启动RocketMQ生产者！");
    }

    /**
     * 初始化生产者
     *
     * @return
     */
    public Producer getProducer() {
        return producer;
    }

}
